﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web.Hosting;
using System.Web;


namespace WcfDataService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "EmployeeService" in code, svc and config file together.
    public class EmployeeService : IEmployeeService
    {
        
        public EmployeeDetails GetEmployeeDetails(int empId)
        {
            EmployeeDetails e = new EmployeeDetails();
            NORTHWNDEntities db = new NORTHWNDEntities();

            var emp = (from i in db.Employees
                           where i.EmployeeID == empId
                       select i).Single();

            e.Name = emp.FirstName;
            e.LastName = emp.LastName;
            e.Photo = emp.Photo;
            e.PhotoUrl = GetPhotoPath(emp.PhotoPath, empId);//emp.PhotoPath;"employeeimage?";send bytes til ihttphandler ?
            return e;
        }

        private string GetPhotoPath(string imagePath, int employeeId)
        {
            string[] aImagePath = imagePath.Split('/');
            string imgUrl = aImagePath[aImagePath.Length-1];

            string employeeImageFolder = System.Configuration.ConfigurationManager.AppSettings["EmployeeImageFolder"].ToString();
            string tmp = employeeImageFolder + "/" + imgUrl;//--->    /Images/Employees/employeeb1c27216-eadc-4c38-a910-288a65140c30.png

            FileInfo fileInf = new FileInfo(HostingEnvironment.MapPath(tmp));

            if (!fileInf.Exists)
            {
                //billedet af medarbejderen fandtes ikke, opret et nyt ud fra billedet i db - gem det i filsystemet og opdatér PhotoUrl i db
                NORTHWNDEntities db = new NORTHWNDEntities();
                var emp = (from i in db.Employees
                           where i.EmployeeID == employeeId
                           select i).Single();

                Byte[] img = (Byte[])emp.Photo;
                MemoryStream ms = new MemoryStream();
                int offset = 78;
                ms.Write(img, offset, img.Length - offset);
                Bitmap bmp = new Bitmap(ms);

                string imageName = "employee" + System.Guid.NewGuid() + ".png";
                string newImageUrl = HostingEnvironment.MapPath(employeeImageFolder + "/" + imageName);

                string s = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];

                bmp.Save(@newImageUrl, System.Drawing.Imaging.ImageFormat.Png);

                imgUrl = imageName;
                //gem den nye uri i db eks: employee68265091-f8a0-402e-9cd6-231d23abb09d.png
                emp.PhotoPath = imageName;
                db.SaveChanges();

                bmp.Dispose();
                ms.Close();
                ms.Dispose();
            }
            return imgUrl;
        }


        public List<string> GetEmployeeNames()
        {
            List<string> names = new List<string>();
            names.Add("Lars");
            names.Add("Ulrik");
            return names;
        }


    }
}
